<?php

require_once(dirname(__DIR__) . '/api/DB.php');
require_once(dirname(__DIR__) . '/api/response.php');
require_once(dirname(__DIR__) . '/api/func.php');

$_DELETE = [];
if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
    $deleteData = file_get_contents('php://input');
    parse_str($deleteData, $_DELETE);
}

try {

    $db = new DB();
    $pdo = $db::$pdo;

    if (!isset($_DELETE['article_id'])) {
        throw new Exception(fieldMap('article_id') . '为必传项');
    }

    if (empty($_DELETE['article_id'])) {
        throw new Exception(fieldMap('article_id') . '不能为空');
    }

    // 判断文章是否存在
    $sql = 'select * from articles where id = ?';
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$_DELETE['article_id']]);
    if ($stmt->rowCount() <= 0) {
        throw new Exception('文章不存在');
    }

    // 删除文章
    $sql = 'delete from articles where id = ?';
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$_DELETE['article_id']]);
    if ($stmt->rowCount() <= 0) {
        throw new Exception('删除失败');
    }

    myResponse('', 1, 200);
} catch (Exception $e) {
    myResponse($e->getMessage(), 0, 200);
}